<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class PurchaseTmp extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_purchase_tmp';


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:17
     * @功能说明:列表
     */
    public function tmpList($dis,$page){

        $data = $this->where($dis)->order('top desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 16:28
     * @功能说明:首页列表
     */
    public function indexTmpList($dis,$mapor,$page){

        $data = $this->alias('a')
                ->join('longbing_card_purchase_tmp_company b','a.id = b.tmp_id','left')
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-09 16:28
     * @功能说明:首页列表
     */
    public function  userIndexTmpList($dis,$mapor,$page){

        $data = $this->alias('a')
            ->join('longbing_card_purchase_tmp_company b','a.id = b.tmp_id','left')
            ->join('longbing_card_purchase_user_tmp c','a.id = c.tmp_id')
            ->where($dis)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->field('a.*,c.staff_id')
            ->group('c.tmp_id,c.staff_id')
            ->order('a.top desc,a.id desc')
            ->paginate($page)
            ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as $k=>$v){

                $data['data'] [$k]['staff_name'] = lbUserName($v['staff_id']);

            }
        }

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:18
     * @功能说明:添加
     */
    public function tmpAdd($data){


        $company = $data['company'];

        unset($data['company']);

        $cate = $data['cate'];

        unset($data['cate']);

        Db::startTrans();

        $data['create_time'] = time();

        $data['update_time'] = time();

        $this->insert($data);

        $id = $this->getLastInsID();

        $res = $this->updateSome($id,$data['uniacid'],$company,$cate);

        Db::commit();

        return $res;

    }


    public function tmpUpdate($dis,$data){

        $company = $data['company'];

        unset($data['company']);

        $cate = $data['cate'];

        unset($data['cate']);

        $data['update_time'] = time();

        Db::startTrans();

        $this->where($dis)->update($data);

        $res = $this->updateSome($data['id'],$data['uniacid'],$company,$cate);

        Db::commit();

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:25
     * @功能说明:
     */
    public function updateSome($id,$uniacid,$company,$cate){

        $company_model = new PurchaseCompany();

        $cate_model    = new PurchaseCate();

        $company_model->dataDel(['tmp_id'=>$id]);

        $cate_model->dataDel(['tmp_id'=>$id]);

        if(!empty($company)){

            $company = $this->changeData($company);

            foreach ($company as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'tmp_id'  => $id,

                    'company_id' => $value

                ];

                $company_model->dataAdd($insert);
            }

        }

        if(!empty($cate)){

            $cate = $this->changeData($cate);

            foreach ($cate as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'tmp_id'  => $id,

                    'cate_id' => $value

                ];

                $cate_model->dataAdd($insert);
            }

        }

        return true;

    }

    /**
     * User: chenniang
     * Date: 2019-09-25 15:29
     * @return void
     * descption:转换插入数据
     */
    public function changeData($ids){
        $data = [];
        if(is_array($ids)){

            foreach ($ids as $k =>$v){
                $data[] = array_pop($v);
            }
        }

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:33
     * @功能说明:列表
     */
    public function tmpInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];
    }






}